package edu.bbu.bibliospring.api.cdiassembler;

import edu.bbu.bibliospring.api.resources.TestResource;
import edu.bbu.bibliospring.backend.model.Message;
import edu.bbu.bibliospring.common.dto.MessageDto;
import org.apache.log4j.Logger;

/**
 * Created by erika on 6/13/14.
 * Assembler class transforming the model and dto of the Message entity
 */
public class MessageAssembler implements Assembler<Message, MessageDto> {

    private static final Logger LOG = Logger.getLogger(TestResource.class);

    @Override
    public Message createModel() {
        LOG.info("MessageAssembler - new Message created");
        return new Message();
    }

    @Override
    public MessageDto createDto() {
        LOG.info("MessageAssembler - new MessageDto created");
        return new MessageDto(Message.class.getName());
    }

    @Override
    public MessageDto modelToDto(Message model) {
        LOG.info("MessageAssembler - modelToDto");
        MessageDto dto = createDto();
        dto.setId(model.getId());
        dto.setMessageContent(model.getMessageContent());
        dto.setSentOn(model.getSentOn());
        ProfileAssembler profileAssembler = new ProfileAssembler();
        dto.setReceiver(profileAssembler.modelToDto(model.getReceiver()));
        LocationAssembler locationAssembler = new LocationAssembler();
        dto.setSenderLibrary(locationAssembler.modelToDto(model.getSenderLibrary()));
        return dto;
    }

    @Override
    public Message dtoToModel(MessageDto dto) {
        LOG.info("MessageAssembler - dtoToModel");
        Message message = createModel();
        message.setId(dto.getId());
        message.setMessageContent(dto.getMessageContent());
        message.setSentOn(dto.getSentOn());
        ProfileAssembler profileAssembler = new ProfileAssembler();
        message.setTo(profileAssembler.dtoToModel(dto.getReceiver()));
        LocationAssembler locationAssembler = new LocationAssembler();
        message.setSenderLibrary(locationAssembler.dtoToModel(dto.getSenderLibrary()));
        return message;
    }
}
